package com.naza.rpc.ratelimit;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * 并发时刻流控
 *
 */
public class SemaRatelimiter extends RateLimiter {

    Semaphore semaphore;

    public SemaRatelimiter(Long maxPermits){
        super(maxPermits);
        semaphore = new Semaphore(maxPermits.intValue());
    }

    @Override
    public void acquire() {
        try {
            semaphore.acquire();
        }catch (Exception e){

        }
    }

    @Override
    public void acquire(long timeout, TimeUnit unit) {
    }

    @Override
    public long tryAcquire() {
        return semaphore.tryAcquire()==true?1L:0;
    }

    @Override
    public void release() {
        semaphore.release();
    }
}
